package com.wang.utils;


import java.io.*;

public class IOUtils {
    public static String readAll(InputStream is, int length) throws IOException {
        if (length == -1) throw new IOException();
        byte[] bs = new byte[length];
        if (is.read(bs, 0, length) == -1) throw new IOException();
        return new String(bs);
    }

    public static String readAll(File file) throws IOException {
        Reader reader = new FileReader(file);
        return readAll(reader);
    }

    public static String readAll(String filename) throws IOException {
        Reader reader = new FileReader(filename);
        return readAll(reader);
    }

    private static String readAll(Reader reader) throws IOException {
        StringBuilder sb = new StringBuilder();
        char[] chs = new char[1024];
        while (reader.read(chs) != -1) {
            sb.append(chs);
            chs = new char[1024];
        }
        reader.close();
        return sb.toString();
    }

    public static void writeAll(String filename, String s) throws IOException {
        Writer writer = new FileWriter(filename);
        writer.write(s);
        writer.flush();
        writer.close();
    }

}
